package com.bbcare.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ToolUtil;

/**
 * @Description: TODO
 * @author MLi
 * @date 2018年4月9日
 * @version V1.0
 */
public class OperationNotNullInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            OperationNotNull operationRecord = handlerMethod.getMethodAnnotation(OperationNotNull.class);
            if (operationRecord != null) {
                String argName = operationRecord.argName();
                String body = new String(((WrappedHttpServletRequest) request).getBytes(), "UTF-8");
                JSONObject jsonArg = JSON.parseObject(body);
                Map<String, Object> data = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);

                // 操作信息校验
                Map<String, Object> chkVar = new HashMap<String, Object>();
                Map<String, Object> chkInfo = ToolUtil.typeCast(data.get(argName));
                chkVar.put("opCode", "操作编码");
                chkVar.put("opType", "操作类型");
                chkVar.put("opNote", "操作备注");
                chkVar.put("channelType", "渠道类型");
                chkVar.put("channelId", "渠道编码");
                ToolUtil.checkInputInfo(argName, chkInfo, chkVar);
            }
        }
        return super.preHandle(request, response, handler);
    }

}
